package splug.cjk;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.ArrayList;

import splug.cjk.packet.FriendPacket;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ReportSimple extends Activity {

	private String id;
	private String author;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.report_tab1_simple);
		setTitle("신고 사유를 적어주세요");

		//신고하는 사람과 신고 당하는 사람의 아이디를 받아온다
		Bundle b = this.getIntent().getExtras();
		id = b.getString("ID");
		author = b.getString("AUTHOR");

		Button ok = (Button) findViewById(R.id.btn_simplereport_ok);
		Button cancel = (Button) findViewById(R.id.btn_simplereport_cancel);
		final EditText reason = (EditText) findViewById(R.id.text_simplereport_reason);

		//신고하기 버튼에 이벤트 추가
		ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//신고 사유를 스트링으로 받아온다
				String str = reason.getText().toString();
				if(str.equals(""))
					Toast.makeText(ReportSimple.this, "신고 사유를 써주세요", Toast.LENGTH_SHORT).show();

				else{
					//신고하기 요청 번호는 22번
					try {
						Socket socket = new Socket(LetsGoActivity.server_ip, 2007);

						InputStream is = socket.getInputStream();
						OutputStream os = socket.getOutputStream();

						OutputStreamWriter osw = new OutputStreamWriter(os);
						InputStreamReader isr = new InputStreamReader(is);

						osw.write(22);
						osw.flush();

						int ack = isr.read(); 

						if(ack == '1'){
							//FriendPacket을 재활용 한다
							ArrayList<String> text = new ArrayList<String>();
							text.add(str);
							FriendPacket packet = new FriendPacket(text, id, author);
							ObjectOutputStream output = new ObjectOutputStream(os);
							output.writeObject(packet);

							ack = isr.read();

							if(ack == '1'){
								Toast.makeText(ReportSimple.this, "신고 완료!", Toast.LENGTH_SHORT).show();
								finish();
							}
							else if(ack == '0'){
								Toast.makeText(ReportSimple.this, "해당하는 회원을 찾을 수 없습니다", Toast.LENGTH_SHORT).show();
							}
							else if(ack == '2'){
								Toast.makeText(ReportSimple.this, "이미 신고를 하였습니다", Toast.LENGTH_SHORT).show();
							}
						}
						else{
							Toast.makeText(ReportSimple.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
							finish();
						}


					} catch (Exception e) {
						Toast.makeText(ReportSimple.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
						finish();
					}
				}
			}
		});

		//뒤로가기 버튼에 이벤트 추가
		cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

}
